<?php

require_once 'mysql.php';
require_once 'entity_murid.php';
session_start();
/*
 * ISI $_POST
 * -halaman : posisi halaman
 * -perintah : insert atau submit
 * -radio?
 *
 * ISI $_SESSION
 *  -jawaban_guru
 */
//$hlmn = $_POST['halaman'];
$command = $_POST['command'];
$e_murid = new jawabanMurid();

$jumlahSoal = $jumlah_soal_murid;
$dbg = $_GET['debug'];

if (isset($command) || $dbg) {
    //update object jawaban guru dari session
    if (isset($_SESSION['jawaban_murid'])) {
        $e_murid = unserialize($_SESSION['jawaban_murid']); // ambil dari session
        if ($e_murid->gettJumlahSoal() != $jumlahSoal) { //cek jumlah soalnya sama atau tidak
            $e_murid->destroyJawaban(); //jawaban tidak valid
            $e_murid = new jawabanMurid(); // buat object baru
        }
    } else {
        //object baru karena tidak ada di session
        $e_murid->setJumlahSoal($jumlahSoal); //jumlah pertanyaan untuk guru
    }
    echo "halaman {$_POST['halaman']} \n";
    switch ($command) {
        case "new":
            insertIdentitasMurid();
        case "submit":
            insertJawaban();
            $_SESSION['jawaban_murid'] = serialize($e_murid); //simpan lagi ke session
            break;
        case "finish":
            insertJawaban();
            submitToDatabase();
        //SQL untuk masukin jawaban ke tabel jangan dibreak;
        case "clear":
            clearSession();
            break;
        default:
            echo "ketangkap\n";
            if ($dbg) {
                echo "bocorkan jawaban guru\n";
                echo "{$_SESSION['jawaban_murid']}\n<p>";
                foreach ($e_murid->getAllJawaban() as $key => $value) {
                    echo "\$jawban [$key] => $value \n<p>";
                }
                echo "SQL = {$e_murid->getSQLInsert()}";
            }
            break;
    }
}

function insertIdentitasMurid() {
    global $e_murid;
    //if (isset ( $_POST['Nama']) && isset ( $_POST['Kelas']) && isset ( $_POST['Sekolah'])){
    $e_murid->setNama(trim($_POST['Nama']));
    $e_murid->setSekolah(trim($_POST['Sekolah']));
    $e_murid->setKelas(trim($_POST['Kelas']));
    //}
}

function insertJawaban() {
    global $e_murid;
    foreach ($_POST as $key => $value) {
        echo "[$key] => $value \n"; //debug
        if (substr($key, 0, 5) == "radio") {
            $i = intval(substr($key, 5)) + 1;
            if ($i > 0) {
                $e_murid->setJawaban($i, $value);
            }
        }
    }
}

function clearSession() {
    global $e_murid;
    if (isset($e_murid)) {
        $e_murid->destroyJawaban();
        unset($e_murid);
    }
    $_SESSION = array();
    if (ini_get("session.use_cookies")) {
        $params = session_get_cookie_params();
        setcookie(session_name(), '', time() - 42000,
                $params["path"], $params["domain"],
                $params["secure"], $params["httponly"]
        );
    }
    session_destroy();
}

function submitToDatabase() {
    global $e_murid;
    global $username, $database, $password;
    $sql = new mysqli('127.0.0.1', $username, $password, $database) or die(mysqli_error($sql));
    //sql->init();
    //$sql->real_connect(
    $sql->multi_query($e_murid->getSQLInsert());
    $sql->close();
}

?>
